package com.xingkeduo.business.order.remote;

import com.sun.tools.javac.util.Log;
import com.xingkeduo.business.order.remote.feign.StoreFeignService;
import com.xingkeduo.dto.ServprodTable;
import com.xingkeduo.dto.ShopsDTO;
import com.xingkeduo.dto.ShopsServprodDTO;
import com.xingkeduo.dto.StoreRemoteDto;
import com.xingkeduo.utils.ResponseUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * @author chi  2019-03-13 10:43
 **/
@Service
@Slf4j
public class StoreRemoteService {



    @Resource
    private StoreFeignService storeFeignService;


    @Cacheable(value = "cache:order:getByStoreId", key = "#storeId")
    public StoreRemoteDto getByStoreId(String storeId){
        log.info("getByStoreId:{}", storeId);
        try {
            ResponseUtils<ShopsDTO> responseUtils = storeFeignService.doGet(Long.valueOf(storeId));
            if(responseUtils!=null && responseUtils.success()){
                ShopsDTO shopsDTO= responseUtils.getData();
                return StoreRemoteDto.builder()
                        .storeId(shopsDTO.getId()+"")
                        .storeName(shopsDTO.getName())
                        .cityId(String.valueOf(shopsDTO.getCitiesId()))
                        .cityName(shopsDTO.getCitiesName())
                        .branchOfficeId(String.valueOf(shopsDTO.getOfficeId()))
                        .branchOfficeName(shopsDTO.getOfficeName())
                        .vendorId(String.valueOf(shopsDTO.getVendorId()))
                        .vendorName(shopsDTO.getVendorName())
                        .storeAddress(shopsDTO.getAddress())
                        .daysList(shopsDTO.getDaysList())
                        .kouBeiShopsNum(shopsDTO.getKouBeiShopsNum())
                        .alipayStoreId(shopsDTO.getAlipayStoreId())
                        .build();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    /**
     * 获取店铺信息，根据服务类型获取店铺价格
     * @param storeId
     * @param serverType
     * @return
     */
    public StoreRemoteDto queryStore(String storeId,String serverType){
        ResponseUtils<ShopsDTO> responseUtils = storeFeignService.doGet(Long.valueOf(storeId));
        if(responseUtils!=null && responseUtils.success()){
            ShopsDTO shopsDTO= responseUtils.getData();
            List<ShopsServprodDTO> shopsServprodList1 = shopsDTO.getShopsServprodList();
            BigDecimal bigdecimal = null;
            if(StringUtils.isNotBlank(serverType)){
                for (ShopsServprodDTO s:shopsServprodList1) {
                    if(serverType.equals(s.getServprodId()+"")){
                        bigdecimal=new BigDecimal(s.getPrice());
                    }
                }
            }
            return StoreRemoteDto.builder()
                    .storeId(shopsDTO.getId()+"")
                    .storeName(shopsDTO.getName())
                    .cityId(String.valueOf(shopsDTO.getCitiesId()))
                    .cityName(shopsDTO.getCitiesName())
                    .branchOfficeId(String.valueOf(shopsDTO.getOfficeId()))
                    .branchOfficeName(shopsDTO.getOfficeName())
                    .vendorId(String.valueOf(shopsDTO.getVendorId()))
                    .vendorName(shopsDTO.getVendorName())
                    .storeAddress(shopsDTO.getAddress())
                    .daysList(shopsDTO.getDaysList())
                    .storePrice(bigdecimal)
                    .build();
        }
        return null;
    }


    /**
     * 获取服务名称
     * @param id
     * @return
     */
    public String getServprod(String id){
        ResponseUtils<ServprodTable>  responseUtils = storeFeignService.getServprod(Long.valueOf(id));
        if(responseUtils.success()){
            return responseUtils.getData().getName();
        }
        return null;
    }


}
